package com.yanqu.road.dao.impl.activity.turtle;

import com.alibaba.fastjson.JSON;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.turtle.data.TurtleGame;
import com.yanqu.road.entity.activity.turtle.data.TurtleUserData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author : Cxp
 * @Time : 2024-09-09
 * @Description : 乌龟对对碰-User Dao
 */

public class TurtleUserDataDaoImpl extends TempDataObjectDao<TurtleUserData> {
    public static String getTurtleUserDataSql = "select * from `t_u_turtle_data` where `activityId`=? and `userId`=?";

    public TurtleUserData getTurtleUserData(int activityId, long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getTurtleUserDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setLong(i, userId);
            rs = call.executeQuery();
            if (rs.next()) {
                return parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public static String updateTurtleUserDataSql = "update `t_u_turtle_data` set score=?,isPlaying=?,badTemplateCnt=?,roundCnt=?,curGame=? where activityId=? and userId=?";

    @Override
    protected boolean updateData(TurtleUserData turtleUserData) {
        turtleUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<>();
        para.put(i++, new DbParameter(Types.BIGINT, turtleUserData.getScore()));
        para.put(i++, new DbParameter(Types.BOOLEAN, turtleUserData.getIsPlaying()));
        para.put(i++, new DbParameter(Types.INTEGER, turtleUserData.getBadTemplateCnt()));
        para.put(i++, new DbParameter(Types.INTEGER, turtleUserData.getRoundCnt()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(turtleUserData.getCurGame())));
        para.put(i++, new DbParameter(Types.INTEGER, turtleUserData.getActivityId()));
        para.put(i, new DbParameter(Types.BIGINT, turtleUserData.getUserId()));

        return getDb().executeNoneQuery(updateTurtleUserDataSql, para) > 0;
    }

    public static String insertTurtleUserDataSql = "insert into `t_u_turtle_data`(activityId,userId,score,isPlaying,badTemplateCnt,roundCnt,curGame) values (?,?,?,?,?,?,?)";

    @Override
    protected boolean addData(TurtleUserData turtleUserData) {
        turtleUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<>();
        para.put(i++, new DbParameter(Types.INTEGER, turtleUserData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, turtleUserData.getUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, turtleUserData.getScore()));
        para.put(i++, new DbParameter(Types.BOOLEAN, turtleUserData.getIsPlaying()));
        para.put(i++, new DbParameter(Types.INTEGER, turtleUserData.getBadTemplateCnt()));
        para.put(i++, new DbParameter(Types.INTEGER, turtleUserData.getRoundCnt()));
        para.put(i, new DbParameter(Types.VARCHAR, JSON.toJSONString(turtleUserData.getCurGame())));

        return getDb().executeNoneQuery(insertTurtleUserDataSql, para) > 0;
    }

    @Override
    protected TurtleUserData parseData(ResultSet rs) throws SQLException {
        TurtleUserData turtleUserData = new TurtleUserData();

        turtleUserData.setActivityId(rs.getInt("activityId"));
        turtleUserData.setUserId(rs.getLong("userId"));
        turtleUserData.setScore(rs.getLong("score"));
        turtleUserData.setIsPlaying(rs.getBoolean("isPlaying"));
        turtleUserData.setBadTemplateCnt(rs.getInt("badTemplateCnt"));
        turtleUserData.setRoundCnt(rs.getInt("roundCnt"));
        turtleUserData.setCurGame(JSON.parseObject(rs.getString("curGame"), TurtleGame.class));

        return turtleUserData;
    }
}
